from django.shortcuts import render
from django.http import JsonResponse,HttpResponse
from rest_framework.decorators import action
from rest_framework.viewsets import ModelViewSet
from .serializers import *
from .models import Interview
from django_filters import rest_framework as filters
# from rest_framework
# Create your views here.
from collections import OrderedDict

from rest_framework.pagination import PageNumberPagination

# 自定义分页器
from rest_framework.response import Response


class MyPageNumberPagination(PageNumberPagination):
    page_size = 3  # 每页显示3条数据
    page_size_query_param = 'size'

    # page_size_query_description = 'size'
    # max_page_size = 5  # 最大每页显示五条数据

    # 重写方法 修改返回值
    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('count', self.page.paginator.count),
            # ('next', self.get_next_link()),
            # ('previous', self.get_previous_link()),
            ('data', data)
        ]))

# 面试过滤器
class InterviewFilter(filters.FilterSet):
    name = filters.CharFilter(lookup_expr='contains')
    company = filters.CharFilter(lookup_expr='contains')
    # fields = {
    #     'name':['icontains']
    # }
    class Meta:
        model = Interview
        fields = '__all__'
        exclude = ['record_src']
        ordering = 'name'  ##加入ordering排序


# 面试
class InterviewModelViewSet(ModelViewSet):
    # 查询集
    queryset = Interview.objects.all()
    # 序列化器
    serializer_class = InterviewSerializer
    # 过滤器
    filter_backends = (filters.DjangoFilterBackend,)
    # filterset_fields = ('name',)
    filterset_class = InterviewFilter
    # @action(methods=['post'],detail=False,url_path='upload')
    # def upload(self,request):
    #     file = request.data
    #     f = open(file['aa'],'rb')
    #     print(f)
    #
    #     return JsonResponse({'code':200})
    pagination_class = MyPageNumberPagination
